package omg.ligong.server;

import java.io.File;

import org.apache.commons.io.FilenameUtils;

/**
 * 配个目录用来管理图片，临时文件等
 * 
 * @author weishuang
 * 
 * 2011-4-23 上午12:35:32
 */
public class WorkDir {
	private String dir;
	private static WorkDir _ins = null;
	private String tempDir;
	private String studentDir;
	private String teacherDir;
	private String adminDir;

	private WorkDir() {
	}

	public static WorkDir getInstance() {
		if (_ins == null) {
			synchronized (WorkDir.class) {
				if (_ins == null) {
					_ins = new WorkDir();
				}
			}
		}
		return _ins;
	}

	public void repair() throws Exception {
		File f = new File(dir);
		if (!f.exists()) {
			f.mkdirs();
		}
		String regDir = FilenameUtils.normalize(dir);
		this.tempDir = regDir + File.separator + "temp";
		f = new File(this.tempDir);
		if (!f.exists()) {
			f.mkdirs();
		}
		this.studentDir = regDir + File.separator + "student";
		f = new File(this.studentDir);
		if (!f.exists()) {
			f.mkdirs();
		}
		this.teacherDir = regDir + File.separator + "teacher";
		f = new File(this.teacherDir);
		if (!f.exists()) {
			f.mkdirs();
		}
		this.adminDir = regDir + File.separator + "admin";
		f = new File(this.adminDir);
		if (!f.exists()) {
			f.mkdirs();
		}
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getTempDir() {
		return this.tempDir;
	}

	public String getDir() {
		return dir;
	}

	public String getStudentDir() {
		return studentDir;
	}

	public String getTeacherDir() {
		return teacherDir;
	}

	public String getAdminDir() {
		return adminDir;
	}

	public static void releaseInstance() {
		_ins = null;
	}
}
